import request from '@/utils/request'

export default {
  getUserList(searchModel) {
    return request({
      url: '/user/list',
      method: 'get',
      params: {
        pageNo: searchModel.pageNo,
        pageSize: searchModel.pageSize,
        username: searchModel.username,
        phone: searchModel.phone
      }
    })
  },
  getCrawlerList(searchModel) {
    return request({
      url: '/crawler/list',
      method: 'get',
      params: {
        pageNo: searchModel.pageNo,
        pageSize: searchModel.pageSize,
        announcementName: searchModel.announcementName,
        announcementNumber: searchModel.announcementNumber,
        resource: searchModel.resource
      }
    })
  },
  addUser(user) {
    return request({
      url: '/user',
      method: 'post',
      data: user
    })
  },
  saveCrawler(crawler) {
    return request({
      url: '/crawler/add',
      method: 'post',
      data: crawler
    })
  },
  updateUser(user) {
    return request({
      url: '/user',
      method: 'put',
      data: user
    })
  },
  saveUser(user) {
    if (user.id == null && user.id === undefined) {
      return this.addUser(user)
    }
    return this.updateUser(user)
  },
  getUserById(id) {
    return request({
      //url: '/user/' + id,
      url: `/user/${id}`,
      method: 'get'
    })
  },
  deleteUserById(id) {
    return request({
      url: `/user/${id}`,
      method: 'delete'
    })
  },
  updatePassword(form) {
    return request({
      url: `/user/updatePassword`,
      method: 'get',
      params: {
        oldPassword: form.oldPassword,
        newPassword: form.newPassword,
        againPassword: form.againPassword
      }
    })
  },
  getUserInfo() {
    return request({
      url: `/user/userInfo`,
      method: 'get'
    })
  },
  updateUserInfo(user) {
    return request({
      url: '/user/updateUserInfo',
      method: 'put',
      data: user
    })
  },
  getCrawlerConfigList(searchModel) {
    return request({
      url: '/crawler/config/list',
      method: 'get',
      params: {
        pageNo: searchModel.pageNo,
        pageSize: searchModel.pageSize,
        descName: searchModel.descName
      }
    })
  },
  saveCrawlerConfig(config) {
    if (config.id == null && config.id === undefined) {
      return this.addCrawlerConfig(config)
    }
    return this.updateCrawlerConfig(config)
  },
  addCrawlerConfig(config) {
    return request({
      url: '/crawler/config/add',
      method: 'post',
      data: config
    })
  },
  updateCrawlerConfig(config) {
    return request({
      url: '/crawler/config/update',
      method: 'put',
      data: config
    })
  },
  getCrawlerConfigById(id) {
    return request({
      url: `/crawler/config/${id}`,
      method: 'get'
    })
  },
  deleteCrawlerConfig(id) {
    return request({
      url: `/crawler/config/delete/${id}`,
      method: 'delete'
    })
  },
  getCrawlerConfigSpecial() {
    return request({
      url: `/crawler/config/special`,
      method: 'get'
    })
  },
  // 新增：用于预览文件的方法
  previewFile(fileId) {
    return request({
      url: '/crawler/file/preview',
      method: 'get',
      params: {
        fileId
      },
      responseType: 'blob' // ⚠️ 必须加这个！因为你要接收的是文件流
    })
  }
}
